import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:video_player/video_player.dart';
import 'package:get/get.dart';
import 'package:xingsu/page/video/model/Video_model.dart';

class VideoHorizontalPage extends StatefulWidget {
  final VideoModel model;

  VideoHorizontalPage(this.model);

  @override
  State<VideoHorizontalPage> createState() => _VideoHorizontalPageState();
}

class _VideoHorizontalPageState extends State<VideoHorizontalPage> {
  late VideoPlayerController _controller;
  bool showToolbar = true;

  @override
  void initState() {
    super.initState();

    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeLeft,
    ]);

    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);

    _controller = VideoPlayerController.network(widget.model.videoPlayURL ?? "")
      ..initialize()
      ..setLooping(true).then((_) {
        _controller.play();

        Future.delayed(const Duration(seconds: 2), () {
          if (showToolbar) {
            setState(() {
              showToolbar = false;
            });
          }
        });
      });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: InkWell(
        onTap: () {
          setState(() {
            showToolbar = !showToolbar;
          });
        },
        child: Stack(
          alignment: Alignment.center,
          children: [
            Center(
              child: AspectRatio(
                aspectRatio: 16 / 9.0,
                child: VideoPlayer(_controller),
              ),
            ),
            showToolbar
                ? Positioned(
                    top: 10,
                    left: 10,
                    child: Container(
                      width: Get.width,
                      height: 40,
                      alignment: Alignment.centerLeft,
                      child: Row(
                        children: [
                          IconButton(
                              onPressed: () {
                                Get.back(result: true);
                              },
                              icon: const Icon(
                                Icons.arrow_back_ios,
                                color: Colors.white,
                              )),
                          Expanded(
                              child: Text(
                            widget.model.videoTitle ?? "",
                            maxLines: 1,
                            style: const TextStyle(color: Colors.white),
                          ))
                        ],
                      ),
                    ))
                : const SizedBox()
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
        overlays: [SystemUiOverlay.top, SystemUiOverlay.bottom]);

    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
    ]);
  }
}
